package com.sz.biz.logistics.ord.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.CollectionUtils;
import com.sz.common.base.utils.StringUtils;
import com.sz.biz.logistics.ord.entity.OrdOrderAddress;
import com.sz.biz.logistics.ord.service.OrdOrderAddressService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Function: 订单地址信息 <br>
 * Author: hpeng.wang. <br>
 * Date: 2017-08-04 17:22:00
 */
@Service("ordOrderAddressService")
public class OrdOrderAddressServiceImpl extends AbstractService implements OrdOrderAddressService {
    @Override
    protected String getMapperNamespace() {
        return "com.sz.biz.logistics.ord.entity.OrdOrderAddressMapper";
    }

    @Override
    public void save(OrdOrderAddress orderAddress) {
        this.batchSave(Arrays.asList(orderAddress));
    }

    @Override
    public void batchSave(List<OrdOrderAddress> ordOrderAddressList) {
        if (!CollectionUtils.isEmpty(ordOrderAddressList)) {
            dao.batchInsert(getSqlName("insert"), ordOrderAddressList);
        }
    }

    @Override
    public void deleteByOrderNo(String orderNo) {
        if (StringUtils.hasLength(orderNo)) {
            dao.delete(getSqlName("deleteByOrderNo"), orderNo);
        }
    }

    /**
     * 更新订单的地址信息
     *
     * @param orderAddress
     * @return
     */
    @Override
    public void update(OrdOrderAddress orderAddress) {
        if (!ObjectUtils.isEmpty(orderAddress)) {
            this.batchUpdate(Arrays.asList(orderAddress));
        }
    }

    @Override
    public void batchUpdate(List<OrdOrderAddress> orderAddressList) {
        if (!CollectionUtils.isEmpty(orderAddressList)) {
            dao.batchUpdate(getSqlName("updateByPrimaryKeySelective"), orderAddressList);
        }
    }

    @Override
    public OrdOrderAddress findByOrderNo(String orderNo) {
        OrdOrderAddress orderAddress = null;
        if (StringUtils.hasLength(orderNo)) {
            List<OrdOrderAddress> orderAddressList = this.findByOrderNos(Arrays.asList(orderNo));
            if (!CollectionUtils.isEmpty(orderAddressList)) {
                orderAddress = orderAddressList.get(0);
            }
        }
        return orderAddress;
    }

    @Override
    public List<OrdOrderAddress> findByOrderNos(List<String> orderNos) {
        List<OrdOrderAddress> orderAddressList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(orderNos)) {
            ParamData pd = new ParamData();
            pd.put("orderNos", orderNos);
            orderAddressList = dao.findForList(getSqlName("findByOrderNos"), pd, OrdOrderAddress.class);
        }
        return orderAddressList;
    }

    /**
     * 根据订单号更新订单号
     *
     * @param oldOrderNo
     * @param newOrderNo
     */
    @Override
    public void updateOrderNoByOrderNo(String oldOrderNo, String newOrderNo) {
        if(StringUtils.isEmpty(oldOrderNo)||StringUtils.isEmpty(newOrderNo)){
            throw Exceptions.bizException(ErrorCodes.ERROR_COMMON,"订单号不能为空！");
        }
        ParamData pd = new ParamData();
        pd.put("newOrderNo",newOrderNo);
        pd.put("oldOrderNo",oldOrderNo);
        dao.update(getSqlName("updateOrderNoByOrderNo"), pd);
    }
}
